import 'package:flutter/material.dart';
import 'tab/home.dart';
import 'tab/history.dart';
import 'tab/my.dart';

class Tabs extends StatefulWidget {
  final int index;
  const Tabs({super.key, this.index = 0});

  @override
  State<Tabs> createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  int _currentIndex = 0;

  final List<Widget> pages = const [
    HomePage(),
    HistoryPage(),
    MyPage(),
  ];

  @override
  void initState() {
    super.initState();
    _currentIndex = widget.index;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '主页',
          style: TextStyle(fontSize: 18, color: Colors.white),
        ),
        backgroundColor: Colors.red,
      ),
      // 根据下标选择页面渲染
      body: pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        // 默认选中的菜单
        currentIndex: _currentIndex,
        // 点击事件
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        // 导航栏按钮大小，默认24
        iconSize: 21,
        // 被选中的按钮颜色，和selectedItemColor参数二选一
        fixedColor: Colors.blue,
        // 导航栏溢出时自适应（一般超出3个菜单时起作用）
        type: BottomNavigationBarType.fixed,
        // 底部导航栏按钮集合
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "首页",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.add),
            label: "学习历史",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: "我的",
          ),
        ],
      ),
      // 浮动按钮
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _currentIndex = 1;
          });
        },
        // 按钮形状
        shape: const CircleBorder(
          side: BorderSide(
            color: Color.fromARGB(255, 116, 224, 243),
            width: 3,
          ),
        ),
        backgroundColor: const Color.fromARGB(255, 196, 255, 59),
        child: const Icon(Icons.school),
      ),
      // 浮动按钮的位置
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      // 左侧抽屉
      drawer: const Drawer(
        width: 200,
        child: Column(
          children: [
            // 可用于用户信息展示的抽屉头部
            UserAccountsDrawerHeader(
              // 当前用户头像
              currentAccountPicture: CircleAvatar(
                backgroundImage: AssetImage("images/2.0x/热巴1.jpg"),
              ),
              // 其他用户头像
              otherAccountsPictures: [
                CircleAvatar(
                  backgroundImage: AssetImage("images/2.0x/热巴1.jpg"),
                ),
              ],
              // 用户名
              accountName: Text("热巴"),
              // 用户邮箱
              accountEmail: Text("123@qq.com"),
              decoration: BoxDecoration(
                color: Colors.purple,
              ),
            ),
            ListTile(
              leading: Icon(Icons.person),
              title: Text("编辑资料"),
            ),
            ListTile(
              leading: Icon(Icons.shopping_bag),
              title: Text("购物车"),
            ),
            ListTile(
              leading: Icon(Icons.book),
              title: Text("订单"),
            ),
            ListTile(
              leading: Icon(Icons.change_circle),
              title: Text("切换账号"),
            ),
            ListTile(
              leading: Icon(Icons.settings),
              title: Text("设置"),
            ),
          ],
        ),
      ),
      // 右侧抽屉
      // endDrawer: ...,
    );
  }
}
